package cn.idongjia.kuaidi.util;

import cn.idongjia.kuaidi.Const;
import cn.idongjia.kuaidi.pojos.ShortTrack;
import cn.idongjia.kuaidi.pojos.Track;
import cn.idongjia.kuaidi.pojos.TrackMessage;
import cn.idongjia.log.Log;
import cn.idongjia.log.LogFactory;
import com.google.gson.Gson;
import com.mongodb.Block;
import com.mongodb.MongoClient;
import com.mongodb.client.FindIterable;
import com.mongodb.client.MongoDatabase;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.bson.Document;
import org.springframework.beans.BeanUtils;

import javax.ws.rs.core.MultivaluedMap;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by dongjia_lj on 16/4/8.
 */
public class Utils {
    private static final Log LOG = LogFactory.getLog(Utils.class);
    private static SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

    public static long getCurrentSecond() {
        return System.currentTimeMillis() / Const.SUM_1000;
    }

    public static String getCurrentFormatDate() {
        return sdf.format(new Date());
    }

    public static String md5(String str, boolean up) {
        if (str != null) {
            try {
                MessageDigest m = MessageDigest.getInstance("MD5");
                m.reset();
                m.update(str.getBytes());
                byte[] digest = m.digest();
                BigInteger bigInt = new BigInteger(1, digest);
                String hashtext = bigInt.toString(Const.SUM_16);
                while (hashtext.length() < Const.SUM_32) {
                    hashtext = "0" + hashtext;
                }
                if (up) {
                    hashtext = hashtext.toUpperCase();
                }
                return hashtext;
            } catch (NoSuchAlgorithmException e) {
                LOG.error(e.getMessage(), e);
            }
        }

        return null;
    }

    public static <T> T toPojo(MultivaluedMap map, Class<T> clazz) {
        try {
            T bean = (T) clazz.newInstance();
            Field[] fields = bean.getClass().getDeclaredFields();
            for (Field field : fields) {
                String varName = field.getName();
                Object object = map.getFirst(varName);
                if (object != null) {
                    try {
                        field.setAccessible(true);
                        field.set(bean, object);

                    } catch (Exception e) {
                        LOG.error(e.getMessage(), e);
                    }
                }
            }
            return bean;
        } catch (Exception e) {
            LOG.error(e.getMessage(), e);
        }

        return null;
    }

    public static void main(String[] args) {
//
//        HttpResponse response = null;
//        HttpClient client = HttpConnectionManager.getHttpClient();
//        HttpPost post = new HttpPost("http://api.kd8.cc/posttest.aspx");
//        post.setHeader("Content_Type", "application/x-www-form-urlencoded");
//        List<NameValuePair> nvps = new ArrayList<NameValuePair>();
//        nvps.add(new BasicNameValuePair("account", "test"));
//        nvps.add(new BasicNameValuePair("data", "申通,668390930489,2013-12-18 17:12:23;"));
//        nvps.add(new BasicNameValuePair("sign", Utils.md5("kd8cctestsecret" + "申通,668390930489," +
//                "2013-12-18 17:12:23;" + "kd8cctestsecret", true)));
//        try {
//            post.setEntity(new UrlEncodedFormEntity(nvps, "utf-8"));
//            response = client.execute(post);
//            String data = EntityUtils.toString(response.getEntity(), "utf-8");
//            System.out.println(data);
//        } catch (IOException e) {
//            System.out.println(e);
//        }

    }
}
